<template>
  <div>

    <el-button type="primary" @click="importExcel(1)" :loading="isShowLoading">导入</el-button>
<!--    <el-button type="primary" @click="openWebSocket">开启</el-button>-->

    <div>
      <div style="margin-top: 10px">控制台输出</div>
      <div style="height: 600px; width: 100%; margin-top: 10px; background: #363636; overflow: scroll; color: white">
        {{websocketParam.logContent}}
      </div>
    </div>

    <el-dialog :visible.sync='dialogShow' :close-on-click-modal='false' :close-on-press-escape='false'>
      <div align="center">
        <p class='tips'>请添加您需要导入的文件</p>
        <el-upload class='upload-demo' ref='upload' drag action='#'
                   :on-remove='handleRemove'
                   :on-exceed='handleExceed'
                   :on-change='handleChange'
                   accept='.xlsx,.xls'
                   :limit='1'
                   :file-list='fileList'
                   :auto-upload='false'>
          <i class='el-icon-upload'></i>
          <div class='el-upload__text'>将文件拖到此处，或<em>点击上传</em></div>
        </el-upload>
      </div>
      <span slot='footer' class='dialog-footer'>
            <el-button @click='downloadTemplate'>下载模版</el-button>
            <el-button type='primary' @click='submitUpload' :loading='isShowLoading'>导入</el-button>
          </span>
    </el-dialog>

  </div>

</template>

<script>
import {fileDownload, importExcel, pendFormData} from "../../../utils/httpRequest";

export default {
  data() {
    return {
      dialogShow: false,
      fileList: [],
      importType: null,
      isShowLoading: false,
      websocketParam: {
        websocket: null,
        messageList: [],
        maxMessages : 20,
        isWebSocketOpen: false,
        logContent: ''
      }
    }
  },
  mounted() {

  },
  methods: {
    downloadTemplate(){
      fileDownload(this.$http.adornUrl('tms/downloadTemplate'), new FormData());
    },
    submitUpload() {
      // 如果上传的文件列表是空的，给出提示
      if (this.fileList.length === 0) {
        return this.$message.warning('请先选择要上传的文件');
      }
      // 因为我们限制一次只上传1个文件，所以取数组里的第一个就可以了
      let file = this.fileList[0];
      // 获取到文件的后缀以便于判断文件类型
      let extension = file.name.substring(file.name.lastIndexOf('.') + 1);
      // 判断文件类型
      console.log(extension)
      if (extension !== 'xlsx' && extension !== 'xls') {
        this.$message.warning('只能上传后缀是xlsx或xls的文件');
        return;
      }
      this.isShowLoading = true
      let formData = new FormData();
      pendFormData(formData, 'file', file);
      pendFormData(formData, 'type', this.importType);
      importExcel(this.$http.adornUrl('tms/importExcel'), formData).then(res => {
        if (res.data.code === 10001) {
          this.$message.success("导入成功")
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error.toLocaleString())
      }).finally(() => {
        this.isShowLoading = false
      })
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
    },
    handleChange(file, fileList) {
      this.fileList.push(file.raw);
    },
    handleExceed(file, fileList) {
      return this.$message.warning('一次只能上传一个文件哦~');
    },
    importExcel(type) {
      this.importType = type
      this.dialogShow = true;
    },
    printLog(content){
      this.websocketParam.logContent += content
      this.websocketParam.logContent += '\n'
      // 如果大于1万个字符，只保留最后的1000个字符
      if (this.websocketParam.logContent.length >= 10000){
        this.websocketParam.logContent
          = this.websocketParam.logContent.substring(8000, 10000)
      }
    },
    openWebSocket() {
      if (this.websocketParam.isWebSocketOpen) {
        this.printLog("连接已打卡")
        return;
      }
      // 判断当前浏览器是否支持WebSocket
      let url = null
      if (process.env.NODE_ENV === 'production'){
        url = "ws://172.20.10.224:8090/onlineMaintance/websocket/logging"
      }else {
        url = "ws://127.0.0.1:8090/onlineMaintance/websocket/logging"
      }
      this.websocketParam.websocket = new WebSocket(url);
      this.websocketParam.websocket.onerror = (event) => {
        this.printLog('websocket连接失败！')
        console.log(event)
      };
      this.websocketParam.websocket.onopen = (event) => {
        this.printLog('websocket连接失成功！')
        this.websocketParam.isWebSocketOpen = true
        console.log(event)
      }
      this.websocketParam.websocket.onmessage = (event) => {
        console.log(event.data)
        this.printLog(event.data)
      };
      this.websocketParam.websocket.onclose = () => {
        this.websocketParam.isWebSocketOpen = false;
        this.printLog('websocket连接关闭')
        console.log('websocket连接关闭')
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
